package cn.xiaopengstack.domain.credit.service.impl;

import cn.xiaopengstack.domain.award.model.valobj.TaskStateVO;
import cn.xiaopengstack.domain.credit.even.CreateAdjustSuccessEvent;
import cn.xiaopengstack.domain.credit.model.aggregate.TradeCreditAggregate;
import cn.xiaopengstack.domain.credit.model.entity.CreditAccountEntity;
import cn.xiaopengstack.domain.credit.model.entity.CreditOrderEntity;
import cn.xiaopengstack.domain.credit.model.entity.TradeCreditEntity;
import cn.xiaopengstack.domain.credit.repository.ICreditRepository;
import cn.xiaopengstack.domain.credit.service.ICreditAdjustService;
import cn.xiaopengstack.domain.strategy.model.entity.UserEntity;
import cn.xiaopengstack.domain.task.model.TaskEntity;
import cn.xiaopengstack.domain.xxx.model.valobj.TradeNameVO;
import cn.xiaopengstack.domain.xxx.model.valobj.TradeTypeVO;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * @author jiangyangang
 */
@Component
@Slf4j
public class CreditAdjustService implements ICreditAdjustService {

    private final ICreditRepository creditRepository;

    public CreditAdjustService(ICreditRepository creditRepository) {
        this.creditRepository = creditRepository;
    }

    @Override
    public String createOrder(TradeCreditEntity tradeCreditEntity) {
        String orderId = RandomStringUtils.randomNumeric(12);

        // 创建MQ补偿任务
        CreateAdjustSuccessEvent<CreateAdjustSuccessEvent.CreateAdjustSuccessEventMessage> createAdjustSuccessEvent = new CreateAdjustSuccessEvent<>(CreateAdjustSuccessEvent.CreateAdjustSuccessEventMessage.builder()
                .userId(tradeCreditEntity.getUserId())
                .outBusinessNo(tradeCreditEntity.getOutBusinessNo())
                .tradeNameVO(TradeNameVO.CONVERT_SKU)
                .build(),
                tradeCreditEntity.getOutBusinessNo());

        TaskEntity taskEntity = TaskEntity.builder()
                .message(JSONObject.toJSONString(createAdjustSuccessEvent.payload()))
                .user(UserEntity.builder().userId(tradeCreditEntity.getUserId()).build())
                .messageId(createAdjustSuccessEvent.getMessage().getId())
                .topic(createAdjustSuccessEvent.topic())
                .state(TaskStateVO.create).build();

        // 调额的判断
        BigDecimal creditAmount;
        TradeTypeVO tradeType = tradeCreditEntity.getTradeType();
        if (TradeTypeVO.REVERSE == tradeType) {
            creditAmount = tradeCreditEntity.getAmount().negate();
        } else {
            creditAmount = tradeCreditEntity.getAmount();
        }

        CreditOrderEntity creditOrderEntity = CreditOrderEntity.builder()
                .userId(tradeCreditEntity.getUserId())
                .tradeAmount(creditAmount)
                .orderId(orderId)
                .tradeName(tradeCreditEntity.getTradeName().getName())
                .tradeType(tradeCreditEntity.getTradeType().getCode())
                .outBusinessNo(tradeCreditEntity.getOutBusinessNo())
                .build();

        // 聚合根
        TradeCreditAggregate aggregate = TradeCreditAggregate.builder()
                .userId(tradeCreditEntity.getUserId())
                .creditOrderEntity(creditOrderEntity)
                .creditAccountEntity(CreditAccountEntity.builder()
                        .userId(tradeCreditEntity.getUserId())
                        .adjustAmount(tradeCreditEntity.getAmount())
                        .build())
                .event(createAdjustSuccessEvent)
                .taskEntity(taskEntity)
                .build();

        creditRepository.saveUserCreditTradeOrder(aggregate);
        return orderId;
    }

    @Override
    public CreditAccountEntity queryUserCreditAccount(String userId) {
        return creditRepository.queryUserCreditAccount(userId);
    }
}
